package com.sj.uabrowser.mvp.video.view.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.classic.common.MultipleStatusView;
import com.orhanobut.logger.Logger;
import com.sj.uabrowser.R;
import com.sj.uabrowser.app.Util;
import com.sj.uabrowser.framework.BaseFragment;
import com.sj.uabrowser.mvp.main.view.adp.MyCustomPagerAdapter;
import com.sj.uabrowser.mvp.video.bean.NVideoItem;
import com.sj.uabrowser.mvp.video.bean.NVideoTab;
import com.sj.uabrowser.mvp.video.contract.VideosContract;
import com.sj.uabrowser.mvp.video.presenter.VideosPresenter;
import com.vondear.rxtool.RxNetTool;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class VideoListFragment extends BaseFragment<VideosContract.Presenter> implements VideosContract.View, TabLayout.BaseOnTabSelectedListener {


    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
//    @BindView(R.id.tv_frg_title)
//    TextView tvFrgTitle;

    List<Fragment> fragments = new ArrayList<>();
    @BindView(R.id.multipleStatusView)
    MultipleStatusView multipleStatusView;

    @BindView(R.id.ll_lishipin)
    LinearLayout llLishipin;
    @BindView(R.id.webview)
    WebView mWebView;

    private String mUrl;
    private int mode;
    private MyCustomPagerAdapter adp;


    @Override
    public VideosContract.Presenter createPresenter() {
        return new VideosPresenter(this);
    }

    @Override
    public int getLayoutID() {
        return R.layout.fragment_video_list;
    }

    @Override
    public void setView() {
        multipleStatusView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (RxNetTool.isAvailable(act)) {
                    fetchTabVidoeList();
                } else {
                    Logger.e("no net.");
                }
            }
        });
    }


    public void fetchTabVidoeList() {
        TreeMap<String, Object> params = new TreeMap<>();
        params.put("mobileName", Util.getDeviceDevice());
        params.put("mobileModel", Util.getDeviceModel());
        params.put("uChannel", Util.getChannelName());
        params.put("cVersion", Util.getVersionName());
        check(params);
        if (presenter != null)
            presenter.fetchTabVideoList(params);
    }

    @Override
    public void setData() {
        if (RxNetTool.isNetworkAvailable(act)) {
//            fetchVideos(0);
            fetchTabVidoeList();
        } else {
            multipleStatusView.showNoNetwork();
        }
    }

    boolean flag = false;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        mWebView.loadUrl(mUrl);
    }


    @Override
    public void showTabVideoList(List<NVideoTab> videoTabs) {
        multipleStatusView.showContent();
        if(fragments.size()>0)return;
//        String tabChannel = videoTab.getTabChannel();

//            List<VideoNavItem> tabNav = videoTab.getTabNav();
        for (int i = 0; i < videoTabs.size(); i++) {
            //添加tab
            NVideoTab newsTabItem = videoTabs.get(i);
            tabLayout.addTab(tabLayout.newTab().setText(newsTabItem.getTag_name()));
            VideoFragment fragment = new VideoFragment();
            Bundle bundle = new Bundle();
            bundle.putString("title", newsTabItem.getTag_name());
            bundle.putString("keys", newsTabItem.getId());
            fragment.setArguments(bundle);
            fragments.add(fragment);
        }
        tabLayout.setupWithViewPager(viewPager);
        adp = new MyCustomPagerAdapter(getChildFragmentManager(), fragments);
        viewPager.setAdapter(adp);
        viewPager.setOffscreenPageLimit(fragments.size());

        for (int i = 0; i < adp.getCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            tab.setCustomView(R.layout.custom_tab_item);
            TextView textView = tab.getCustomView().findViewById(R.id.tab_text);
            textView.setText(videoTabs.get(i).getTag_name());
            if (i == 0) {
                // 设置第一个tab的TextView是被选择的样式
                setTabItemTextStyle(tab, 16, true);

            } else {
                setTabItemTextStyle(tab, 14, false);

            }
        }
        tabLayout.addOnTabSelectedListener(this);
//            if (videoTabs.size() == 1) {
//                tvFrgTitle.setVisibility(View.VISIBLE);
//                tabLayout.setVisibility(View.GONE);
//            } else {
//                tvFrgTitle.setVisibility(View.GONE);
//                tabLayout.setVisibility(View.VISIBLE);
//            }
    }

    private void setTabItemTextStyle(TabLayout.Tab tab, int fontSize, boolean isSelected) {
        tab.getCustomView().findViewById(R.id.tab_text).setSelected(isSelected);
        TextView textView = tab.getCustomView().findViewById(R.id.tab_text);
        textView.setTextSize(fontSize);
        if (isSelected) {
            textView.setTextColor(getResources().getColor(R.color.news_text_selected));
        } else {
            textView.setTextColor(getResources().getColor(R.color.video_tab_unselected));

        }
//        View tabIndicator = tab.getCustomView().findViewById(R.id.tab_indicator);
//        tabIndicator.setVisibility(isSelected ? View.VISIBLE : View.GONE);
        textView.getPaint().setFakeBoldText(isSelected);
        textView.invalidate();
    }

    @Override
    public void showVideosList(List<NVideoItem> videoNav) {
//        multipleStatusView.showContent();
//        List<VideoNavItem> tabNav = videoNav.getTabNav();
//        for (int i = 0; i < tabNav.size(); i++) {
//            //添加tab
//            VideoNavItem newsTabItem = tabNav.get(i);
//            tabLayout.addTab(tabLayout.newTab().setText(newsTabItem.getName()));
//            VideoFragment fragment = new VideoFragment();
//            Bundle bundle = new Bundle();
//            bundle.putString("title", newsTabItem.getName());
//            bundle.putInt("keys", newsTabItem.getKeys());
//            bundle.putSerializable("datas", videoNav.getList());
//            fragment.setArguments(bundle);
//            fragments.add(fragment);
//        }
//        MyPagerAdapter adp = new MyPagerAdapter(getChildFragmentManager(), fragments);
//        viewPager.setAdapter(adp);
//        tabLayout.setupWithViewPager(viewPager);
//        if (tabNav.size() == 1) tabLayout.setVisibility(View.GONE);
    }

    @Override
    public void updateVideoUrl(String url, int position) {

    }

    public void notifyAdpUpdate(){
        if(adp!=null){
            adp.notifyDataSetChanged();
        }
    }


    private void fetchVideos(int cId) {
        TreeMap<String, Object> params = new TreeMap<>();
        params.put("cId", cId);
        params.put("number", 10);
        check(params);
        if (presenter != null)
            presenter.refreshVideos(params);
    }

    @Override
    public void showError(String msg, int errorCode) {

    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        tab.getCustomView().findViewById(R.id.tab_text).setSelected(true);
        setTabItemTextStyle(tab, 17, true);
        viewPager.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
        tab.getCustomView().findViewById(R.id.tab_text).setSelected(false);
        setTabItemTextStyle(tab, 15, false);
    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }



    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        int position = tabLayout.getSelectedTabPosition();
        if (fragments != null && fragments.size() > 0) {
            VideoFragment videoFragment = (VideoFragment) fragments.get(position);
            if (videoFragment != null) videoFragment.onVideoPause();
        }
    }

}
